package net.hctek.identity.action;
	
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;


import com.opensymphony.xwork2.ActionSupport;

import net.hctek.identity.bean.User;
import net.hctek.identity.service.IdentityServiceI;
import net.hctek.identity.util.Contant;
import net.hctek.identity.util.CookieUtils;
import net.hctek.identity.util.MD5;
import net.hctek.identity.util.ResponseJsonUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class UserAction extends ActionSupport{
	

	private static final long serialVersionUID = 1L;
	
	private User user;
	private String tip;

	@Autowired
	private IdentityServiceI identityService;
	
	//查询所以用户
	public String selectUser(){
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			List<User> userAll= identityService.selectUser();
           JSONArray json = new JSONArray();
            for(User u : userAll){
                JSONObject jo = new JSONObject();
                jo.put("name",u.getUserId());
                jo.put("sex", u.getUserSex());
                jo.put("language",u.getUserLanguage());
                jo.put("race", u.getUserRace());
                jo.put("mailbox", u.getUserMailBox());
                jo.put("phoneNumBer", u.getUserPhoneNumBer());
                json.add(jo);
            }
            System.out.println(json.toString());
            ResponseJsonUtils.json(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NONE;
	}
	
	//创建用户
	public String savaUser(){
		try {
			if(identityService.ajaxValidName(user.getUserId())){
				User u = identityService.getUserById(user.getUserId());
				tip = "用户名重复!";
				if(u != null && u.getDelFlag().equals("0")){
					identityService.updateUser(u);
					tip = "该用户以前被删除过，现已复原。";
					
				}
			}else{
				//MD5加密
				String password = MD5.EncoderByMd5(user.getPassWord());
				user.setPassWord(password);
				identityService.savaUser(user);
				//清空账号
				user.setUserId("");
				tip = "创建成功！";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			tip = "创建失败！";
			
		}
		return SUCCESS;
	}
	
	//异步校验账号
/*	public String ajaxValidName(){
			
			try {
				Boolean flag = identityService.ajaxValidName(user.getUserId());
				System.out.println("flag:"+flag);
				ServletActionContext.getResponse().getWriter().print(flag);  
			} catch (Exception e) {
				e.printStackTrace();
				
			}
			return NONE;
		}*/
	
	//根据用户id获取用户信息
	public String showUpdateUser(){
		//System.out.println(user.toString());
		try {
			user = identityService.getUserById(user.getUserId());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();			
		}
		return SUCCESS;
	}
	
	//修改用户
		public String updateUser(){
			
			try {
				tip = identityService.updateUser(user);				
				tip = "修改成功！";
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				tip = "修改失败！";
				
			}
			return SUCCESS;
		}
	//删除用户
		public String deleteUserByIds(){
			try {
				identityService.deleteUserByIds(user.getUserId());
				//tip="删除成功";
			} catch (Exception e) {
				e.printStackTrace();
				//tip="删除失败";
			}
			return SUCCESS;
		}
		
		 /**
	     * 用户退出的业务
	     */
	    public String userLogout(){ 
			
			//清除session中用户信息以及cookie中用户信息
			ServletActionContext.getRequest().getSession().removeAttribute(Contant.SESSION_USER);
		
			//清除cookie信息
			CookieUtils.removeCookie(Contant.REMME);
			return LOGIN;
		}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}
	
	
}
